package com.eastfair.messagecenter.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.eastfair.auth.exceptioncode.AuthServiceExceptionCode;
import com.eastfair.messagecenter.dto.InnerTemplateDTO;
import com.eastfair.messagecenter.entity.InnerTemplate;
import com.eastfair.messagecenter.dto.InnerTemplateSaveDTO;
import com.eastfair.messagecenter.dto.InnerTemplateUpdateDTO;
import com.eastfair.messagecenter.dto.InnerTemplatePageQuery;
import com.eastfair.messagecenter.entity.SmsTemplate;
import com.eastfair.messagecenter.exceptioncode.MessageServiceExceptionCode;
import com.eastfair.messagecenter.service.InnerTemplateService;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.messagecenter.vo.InnerTemplateVO;
import com.eastfair.messagecenter.vo.SmsTemplateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 短信模板
 * </p>
 *
 * @author ligang
 * @date 2022-09-20
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/innerTemplate")
@Api(value = "InnerTemplate", tags = "站内信模板")
@PreAuth(replace = "messagecenter:innerTemplate:")
public class InnerTemplateController extends SuperController<InnerTemplateService, Long, InnerTemplate, InnerTemplatePageQuery, InnerTemplateSaveDTO, InnerTemplateUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<InnerTemplate> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<InnerTemplate> innerTemplateList = list.stream().map((map) -> {
            InnerTemplate innerTemplate = InnerTemplate.builder().build();
            //TODO 请在这里完成转换
            return innerTemplate;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(innerTemplateList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<InnerTemplatePageQuery> params) {
        InnerTemplatePageQuery pageQuery = params.getModel();
    }


    /**
     * 自定义新增
     *
     * @param model 保存对象
     * @return 返回SUCCESS_RESPONSE, 调用默认更新, 返回其他不调用默认更新
     */
    @Override
    public R<InnerTemplate> handlerSave(InnerTemplateSaveDTO model) {
        InnerTemplateDTO innerTemplateDTO = new InnerTemplateDTO();
        innerTemplateDTO.setTemplateCode(model.getTemplateCode());
        innerTemplateDTO.setSystemScene(model.getSystemScene());
        List<InnerTemplate> innerTemplateList = baseService.list(innerTemplateDTO,null);
        if(CollectionUtil.isNotEmpty(innerTemplateList)){
            return R.fail(MessageServiceExceptionCode.TEMPLATE_AGAIN);
        }
        InnerTemplate innerTemplate = new InnerTemplate();
        return R.successDef();
    }



    /**
     * 查询证件动态表单答题详情
     *
     * @param id
     * @return 是否成功
     */
    @ApiOperation(value = "查询证件动态表单答题详情", notes = "查询证件动态表单答题详情")
    @GetMapping("/detail")
    public R<InnerTemplateVO> detail(Long id) {
        return R.success(baseService.doToVo(baseService.getByIdCache(id)));
    }
}
